﻿using System;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using Ninject.Core;
using Ninject.Core.Parameters;
using TestBat.GuyWire.Configurators;
using TestBat.GuyWire.Modules;
using TestBat.IModels.Domain;
using TestBat.Models.Domain;
using TestBat.ServiceLocator;
using uNhAddIns.SessionEasier;

namespace TestBat.SetupDB
{
    class Program
    {
        public static void Main(string[] args)
        {
            SetupSchema();
            SetupData();
        }

        protected static void SetupSchema()
        {
            IKernel kernel = new StandardKernel(new SettingsModule(), new ModelModule());
            DefaultServiceLocator.Kernel = kernel;
            IConfigurationProvider cfg = kernel.Get<IConfigurationProvider>(With.Parameters.Variable("console", true));
            cfg.AfterConfigure += (sender, e) =>
            {
                new SchemaExport(e.Configuration).Execute(false, true, true);
                new SchemaExport(e.Configuration).Execute(false, true, false);
            };

            cfg.Configure();
        }

        protected static void SetupData()
        {
            using (ISession session = DefaultServiceLocator.Get<ISessionFactory>().OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.FlushMode = FlushMode.Commit;

                // taskfolders
                ITaskFolder[] taskFolders = new ITaskFolder[5];
                taskFolders[0] = new RootTaskFolder { Name = "Root" };
                taskFolders[1] = new LeafTaskFolder { Name = "Office 2003", Parent = taskFolders[0] };
                taskFolders[2] = new LeafTaskFolder { Name = "MS Excel", Parent = taskFolders[1] };
                taskFolders[3] = new LeafTaskFolder { Name = "MS Word", Parent = taskFolders[1] };
                taskFolders[4] = new LeafTaskFolder { Name = "MS Powerpoint", Parent = taskFolders[1] };
                SaveArray(session, taskFolders);

                // essaytasks
                IEssayTask[] essayTasks = new IEssayTask[] {
                                                };
                SaveArray(session, essayTasks);

                // multiplechoicetasks
                IMultipleChoiceTask[] multipleChoiceTasks = new IMultipleChoiceTask[3];
                multipleChoiceTasks[0] = new MultipleChoiceTask { Name = "Sum", MaximumPoints = 1, Question = "What does the formula =Sum(A1:B5) calculate?", TaskFolder = taskFolders[2] };
                new Choice { Correct = false, Task = multipleChoiceTasks[0], Text = "The formula is incorrect" };
                new Choice { Correct = true, Task = multipleChoiceTasks[0], Text = "Sums the 10 numbers in the area from A1 to B5" };
                new Choice { Correct = false, Task = multipleChoiceTasks[0], Text = "Multiplies each pair of numbers in A and B and sums the results" };
                multipleChoiceTasks[1] = new MultipleChoiceTask { Name = "###", MaximumPoints = 1, Question = "A cell only displays hashes (#). What does that mean?", TaskFolder = taskFolders[2] };
                new Choice { Correct = true, Task = multipleChoiceTasks[1], Text = "The column is too narrow" };
                new Choice { Correct = false, Task = multipleChoiceTasks[1], Text = "Excel doesn't work correctly -> restart computer" };
                new Choice { Correct = false, Task = multipleChoiceTasks[1], Text = "The formula is incorrect" };
                multipleChoiceTasks[2] = new MultipleChoiceTask { Name = "What is not allowed inside a list?", MaximumPoints = 1, Question = "What is not allowed inside a list?", TaskFolder = taskFolders[2] };
                new Choice { Correct = false, Task = multipleChoiceTasks[2], Text = "Empty rows" };
                new Choice { Correct = false, Task = multipleChoiceTasks[2], Text = "Headers in more than one row" };
                new Choice { Correct = true, Task = multipleChoiceTasks[2], Text = "Empty cells" };

                SaveArray(session, multipleChoiceTasks);

                // multipleresponsetasks
                IMultipleResponseTask[] multipleResponseTasks = new IMultipleResponseTask[] {
                                                    new MultipleResponseTask { Name = "MultipleResponseTask 01", MaximumPoints = 1, Question = "This is MultipleResponseQuestion 01. And this is just a filler. Just a filler. Filling... Just a filler. Filling... Just a filler. Filling...", TaskFolder = taskFolders[0], Grading = MultipleResponseTaskGrading.Ratio },
                                                };
                new Choice { Correct = true, Task = multipleResponseTasks[0], Text = "Choice 01" };
                new Choice { Correct = false, Task = multipleResponseTasks[0], Text = "Choice 02" };
                SaveArray(session, multipleResponseTasks);

                // schedules
                ISchedule[] schedules = {
                                            new Schedule { Name = "Schedule01", Introduction = "", Active = true, Start = new DateTime(2009, 11, 1), End = new DateTime(2010, 12, 1), Time = new TimeSpan(4, 1, 0) },
                                            new Schedule { Name = "Schedule02", Introduction = "", Active = true, Start = new DateTime(2009, 11, 1), End = new DateTime(2009, 12, 1), Time = new TimeSpan(1, 10, 0) },
                                        };
                SaveArray(session, schedules);

                // schedule tasks
                //IScheduleTask[] scheduleTasks = {
                //                                   new ScheduleTask { Schedule = schedules[0], Sort = 3, Task = essayTasks[0] },
                //                                   new ScheduleTask { Schedule = schedules[0], Sort = 1, Task = multipleChoiceTasks[0] },
                //                                   new ScheduleTask { Schedule = schedules[0], Sort = 2, Task = essayTasks[2] },
                //                                   new ScheduleTask { Schedule = schedules[0], Sort = 4, Task = multipleResponseTasks[0] },
                //                                   new ScheduleTask { Schedule = schedules[1], Sort = 1, Task = essayTasks[3] },
                //                                };
                //SaveArray(session, scheduleTasks);

                // trainee groups
                ITraineeGroup[] traineeGroups = {
                                                      new TraineeGroup { Name = "TraineeGroup 01" },
                                                      new TraineeGroup { Name = "TraineeGroup 02" },
                                            };
                SaveArray(session, traineeGroups);

                // trainees
                ITrainee[] trainees = {
                                                      new Trainee { Username = "Trainee 01", Password = "Password01" },
                                                      new Trainee { Username = "Trainee 02", Password = "Password02" },
                                                      new Trainee { Username = "Trainee 03", Password = "Password03" },
                                            };
                trainees[0].TraineeGroups.Add(traineeGroups[0]);
                trainees[1].TraineeGroups.Add(traineeGroups[0]);
                trainees[2].TraineeGroups.Add(traineeGroups[1]);
                SaveArray(session, trainees);

                // schedule traineesgroups
                IScheduleTraineeGroup[] scheduleTraineeGroups = {
                                                                    new ScheduleTraineeGroup { Schedule = schedules[0], TraineeGroup = traineeGroups[0] },
                                                                };
                SaveArray(session, scheduleTraineeGroups);

                ITest[] tests = {
                                    new Test { FinishTime = DateTime.Now, Schedule = schedules[0], StartTime = DateTime.Now - new TimeSpan(0, 10, 0), Trainee = trainees[1] }
                                };
                SaveArray(session, tests);

                ITestTask[] testTasks = {
                                            new TestEssayTask { Test = tests[0], Question = "This is TextQuestion 01. And this is just a filler. Just a filler. Filling... Just a filler. Filling... Just a filler. Filling... Just a filler. Filling... Just a filler. Filling...", MaximumPoints = 1, Sort = 1, Solution = new EssaySolution { Text = "Anwser 01", Percentage = 50 } },
                                            new TestEssayTask { Test = tests[0], Question = "This is TextQuestion 03. And this is just a filler. Just a filler. Filling... Just a filler. Filling... Just a filler. Filling...", MaximumPoints = 3, Sort = 2, Solution = new EssaySolution { Text = "Anwser 02", Percentage = 100 } },
                                            new TestMultipleChoiceTask { Test = tests[0], Question = "This is MultipleChoiceQuestion 01. And this is just a filler. Just a filler. Filling... Just a filler. Filling... Just a filler. Filling...", MaximumPoints = 1, Solution = new MultipleChoiceSolution { } },
                                            new TestMultipleResponseTask { Test = tests[0], Question = "This is MultipleResponseQuestion 01. And this is just a filler. Just a filler. Filling... Just a filler. Filling... Just a filler. Filling...", MaximumPoints = 1, Sort = 3, Solution = new MultipleResponseSolution { }, Grading = MultipleResponseTaskGrading.PlusMinus },
                                        };
                ITestChoice tc = new TestChoice { Correct = true, Task = (ITestChoiceTask)testTasks[2], Text = "Choice 01" };
                new TestChoice { Correct = false, Task = (ITestChoiceTask)testTasks[2], Text = "Choice 02" };
                ((IMultipleChoiceSolution)(testTasks[2]).Solution).Pick = tc;
                tc = new TestChoice { Correct = true, Task = (ITestChoiceTask)testTasks[3], Text = "Choice 01" };
                ITestChoice tc2 = new TestChoice { Correct = false, Task = (ITestChoiceTask)testTasks[3], Text = "Choice 02" };
                new TestChoice { Correct = true, Task = (ITestChoiceTask)testTasks[3], Text = "Choice 03" };
                ((IMultipleResponseSolution)(testTasks[3]).Solution).Picks.Add(tc);
                ((IMultipleResponseSolution)(testTasks[3]).Solution).Picks.Add(tc2);

                SaveArray(session, testTasks);

                transaction.Commit();
            }
        }

        protected static void SaveArray(ISession session, object[] array)
        {
            foreach (var o in array)
                session.Save(o);
        }
    }
}
